#ifndef QPCAPPACKET_H
#define QPCAPPACKET_H

#include <QObject>
#include <QtNetwork/QHostAddress>
#include <QByteArray>
#include <QHostAddress>
#include <QString>



/* 4 bytes IP address */
typedef struct ip_address{
    unsigned char byte1;
    unsigned char byte2;
    unsigned char byte3;
    unsigned char byte4;
}ip_address;

/* IPv4 header */
typedef struct IpHeader{
    unsigned char  VersionAndHeaderLength;   // Type of service
    char trucOnSenFou;             //unknow shit
    unsigned short tlen;           // Total length
    unsigned short identification; // Identification
    unsigned short flags_fo;       // Flags (3 bits) + Fragment offset (13 bits)
    unsigned char  ttl;            // Time to live
    unsigned char  protocol;       // Protocol
    unsigned short crc;            // Header checksum
    ip_address  saddr;      // Source address
    ip_address  daddr;      // Destination address
}IpHeader;

/* UDP header*/
typedef struct UdpHeader{
    unsigned short sourcePort;          // Source port
    unsigned short destinationPort;     // Destination port
    unsigned short len;            // Datagram length
    unsigned short checksum;            // Checksum
}UdpHeader;



class QPcapPacket : public QObject
{
    Q_OBJECT
public:
    explicit QPcapPacket(QObject *parent = 0);
    QPcapPacket(QHostAddress  * sourceAddress, int sourcePort, QHostAddress * destinationAddress, int destinationPort, QByteArray *  data, QObject *parent = 0);
    QPcapPacket(unsigned char * dataGram, QObject *parent = 0);

    ~QPcapPacket();
    QByteArray * getData();
    QString dataToString(int maxLength = 256);
    QString toString();
    QHostAddress * getSourceAddress();
    QHostAddress * getDestinationAddress();

    unsigned short getSourcePort();
    unsigned short getDestinationPort();
    int getProtocol();
    unsigned short getChecksum();
    unsigned short getOpCode();

    static int getIpLengthFromVersion(unsigned char version);
    static QByteArray invertBitsOrder(const QByteArray array);
    static unsigned short invertBitsOrder(const unsigned short number);

    const static int protocolICMP = 1;
    const static int protocolIGMP = 2;
    const static int protocolTCP = 6;
    const static int protocolUDP = 17;
    const static int protocolENCAP = 41;
    const static int protocolOSPF = 89;
    const static int protocolSCTP = 132;

private:
    QHostAddress * sourceAddress;
    int sourcePort;
    QHostAddress * destinationAddress;
    int destinationPort;
    QByteArray * data;
    int protocol;
    unsigned short checksum;

signals:

public slots:

};

#endif // QPCAPPACKET_H
